package com.youth.api;

import com.youth.entity.po.OrderItem;
import com.youth.entity.vo.OrderItemVO;
import com.youth.entity.vo.OrderShippingVO;
import com.youth.entity.vo.OrderVO;
import com.youth.result.ResultBean;
import com.youth.service.OrderService;
import com.youth.service.vo.IOrderVOService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName OrderApi
 * @Description: TODO
 * @Author dy
 * @Date 2019/11/11 11:11
 **/
@RestController
@RequestMapping("/order")
@Api(value = "订单，控制层")
@Slf4j
public class OrderApi implements IOrderVOService {
    @Autowired
    private OrderService orderService;

    @Override
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "订单添加接口", notes = "订单模块使用")
    public ResultBean add(@RequestBody OrderVO orderVO) {
        try {
            if (orderVO.getOrderItemVO() == null || StringUtils.isEmpty(orderVO.getOrderItemVO().getTCId()) ||
                    orderVO.getOrderItemVO().getNum() <= 0) {
                return ResultBean.returnInsertUnSuccess("订单商品信息有误");
            }
            return ResultBean.returnInsertUnSuccess(orderService.add(orderVO));
        } catch (Exception e) {
            log.info("e.getMsg:{}" + e.getMessage());
            return ResultBean.returnInsertUnSuccess(null);
        }

    }

    @Override
    @RequestMapping(value = "/deleteByCode", method = RequestMethod.POST)
    @ApiOperation(value = "订单删除接口", notes = "订单模块使用")
    public ResultBean deleteByCode(String code) {
        return orderService.deleteOrderById(code);
    }

    @Override
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "订单更新接口", notes = "订单模块使用")
    public ResultBean update(OrderVO orderVO) {
        return orderService.updateOrder(orderVO);
    }

    @Override
    @RequestMapping(value = "/findByCode", method = RequestMethod.GET)
    @ApiOperation(value = "订单查询接口", notes = "订单模块使用")
    public ResultBean findByCode(String code) {
        return orderService.queryOrderById(code);
    }

    @Override
    @RequestMapping(value = "/findOrderByUser", method = RequestMethod.POST)
    @ApiOperation(value = "用户订单查询", notes = "订单模块使用")
    public ResultBean getOrdersforUser(String userId) {
        return orderService.getOrdersByUserId(userId);
    }
}